package cn.xzqwjw.taskmanager.common.validator;

import cn.hutool.core.util.IdcardUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author rush
 */
public class EmailValidatorClass implements ConstraintValidator<EmailValidator, Object> {

  @Override
  public void initialize(EmailValidator validator) {
  }

  /**
   * 用户输入的电子邮箱地址
   *
   * @param value 用户输入的值，如从前端传入的某个值
   */
  @Override
  public boolean isValid(Object value, ConstraintValidatorContext cvContext) {
    if (!StringUtils.hasText(value.toString())) {
      return true;
    }
    // 验证邮箱地址合法性
    String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
    Pattern pattern = Pattern.compile(emailRegex);
    Matcher matcher = pattern.matcher(value.toString());
    return matcher.matches();
  }

}